//
//  ViewController.m
//  UIHomeWork09--省市区两级带section
//
//  Created by tusm on 16/7/26.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"
#import "ZoneViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *proArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"省/市";
    [self handleData];
    [self createSubView];
}
- (void)createSubView{

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];

}


- (void)handleData{
    //数据解析:
    NSString *string = [NSString stringWithContentsOfFile:@"/Users/tusm/Downloads/UI-4/HomeWork/09-省市区(两页)/09-省市区(两页)/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *array = [string componentsSeparatedByString:@"\n"];
    self.proArray = [NSMutableArray array];
    for (NSString *str  in array) {
        if (![str hasPrefix:@" "]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            [proDic setObject:str forKey:@"proName"];
            NSMutableArray *cityArray = [[NSMutableArray alloc]init];
            [proDic setObject:cityArray forKey:@"cityArray"];
            [self.proArray addObject:proDic];
        } else if(![str hasPrefix:@"    "]) {
            NSMutableDictionary *proDic = [self.proArray lastObject];
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:str forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            [cityArray addObject:cityDic];
        } else {
            NSMutableDictionary *proDic = [self.proArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *cityDic = [cityArray lastObject];
            NSMutableArray *zoneArray = [cityDic objectForKey:@"zoneArray"];
            [zoneArray addObject:str];
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _proArray.count;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//取字典再取数组
    return [[_proArray[section] objectForKey:@"cityArray"] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    //给cell赋值
    NSMutableDictionary *proDic = _proArray[indexPath.section];
    NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
    NSMutableDictionary *cityDic = cityArray[indexPath.row];
    cell.textLabel.text = [cityDic objectForKey:@"cityName"];
    return cell;
}

//为section设置标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [[_proArray objectAtIndex:section] objectForKey:@"proName"];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZoneViewController *vc = [[ZoneViewController alloc] init];
    NSMutableDictionary *proDic = [_proArray objectAtIndex:indexPath.section];
    NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
    NSMutableDictionary *cityDic = [cityArray objectAtIndex:indexPath.row];
    vc.zoneMedian = [cityDic objectForKey:@"zoneArray"];

    [self.navigationController pushViewController:vc animated:YES];
    




}





















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
